//
// @Author: Zhenwei Xiao
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/8 上午10:04
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const tsp = require("./Transport")

class UdpTransport extends tsp.Transport{
    constructor(t) {
        super(t)
        this.conn = null  // 包含发送方和接收方两个socket
        this.recvBuf = null
        this.remoteUdpAddr = ""
    }
    // Transport
    // conn          *net.UDPConn // UDP句柄
    // recvBuf       []byte       // 接收缓冲区，大小为  9000
    // remoteUdpAddr net.UDPAddr  // 对端UDP地址，用于发送UDP包

    //
    // @Description: 	用于接收数据的transport的初始化函数
    // @receiver u
    // @param conn *net.UDPConn		UDP句俩，包含发送方和接收方两个socket
    // @param udpTransportMap		remoteUri 和 logicFace的映射表
    //
    Init(conn) {
        // console.log("4444444", conn)
        this.conn = conn
        this.localAddr = conn[0].hostFrom  // 发送方的socket
        this.localUri = "udp://" + this.localAddr
        this.remoteAddr = conn[1].hostFrom // 接收方的socket
        this.remoteUri = "udp://" + this.remoteAddr
        this.recvBuf = new Buffer.alloc(9000)
    }

    //
    // @Description: 发送一个lpPacket
    // @receiver u
    // @param lpPacket  *packet.LpPacket
    // @return error
    //
    Send(lpPacket) {
        var encodebuf = this.encodeLpPacket2ByteArray(lpPacket)
        var encodeBufLen = encodebuf[0]
        var encodeBuf = encodebuf[1]
        if (encodeBufLen <= 0) {
            return new Error("encode lpPacket error")
        }
        console.log("encodeBuf :", new Uint8Array(encodeBuf))
        this.conn[0].sendTo(this.remoteAddr,12345 , new Uint8Array(encodeBuf))
    }

    //
    // @Description: 从UDP句柄中接收到UDP包，并返回给上层
    // @receiver u
    // @return *packet.LpPacket, error
    //
    doReceive() {
        var got
        // this.conn.on("message", function (msg, rinfo) {
        //     remoteUdpAddr = rinfo.address
        //     readLen = msg.length
        //     console.log("recv from : ", remoteUdpAddr)
        // })
        // readLen, remoteUdpAddr, err := u.conn.ReadFromUDP(u.recvBuf)
        // if err != nil {
        //     return nil, err
        // }
        try {
            got = this.conn[1].receiveFrom()
            this.recvBuf = got.data.buffer
            this.remoteUdpAddr = got.host
            // console.log("recv from : ", this.remoteUdpAddr)
        }catch (err){
            return [null, err]
        }
        var lpPacket
        try {
            // console.log("this.recvBuf :", this.recvBuf)
            lpPacket = this.parseByteArray2LpPacket(this.recvBuf)
        }catch (err){
            return [null, err]
        }
        return [lpPacket, null]
    }

    Receive() {
        return this.doReceive()
    }
    //
    // @Description: 设置超时时间
    // @param duration int64 小于0则一直保持
    // @return error
    //
    SetReadTimeout(duration) {
        // if (duration <= 0) {
        //     return u.conn.SetReadDeadline(time.Time{})
        // }
        setTimeout(this.conn[0].disconnect(), duration)
        setTimeout(this.conn[1].disconnect(), duration)
        // curTime := time.Now()
        // return u.conn.SetReadDeadline(curTime.Add(time.Duration(duration)))
    }
}

module.exports = {
    UdpTransport: UdpTransport
}

